﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using FLTVAGSAB;
namespace WPFExcelReport
{
    public partial class FLTVAGSABData
    {
        public DataSet Convert2GSCOLOR()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "GSCOLOR_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "GSCOLOR";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "GSCOLOR"
                        select dwMap;
            var query1 = from dwYarn in SetExt.Tables["VAFARBEN_EXT"].AsEnumerable()
                         select dwYarn;
            foreach (DataRow dwYarn in query1)
            {
                EventDispatcher.Instance.RaiseStatus("转换色号到GS07:" + dwYarn["VAFART"].ToString() + "-" + dwYarn["VAFFARBE"].ToString(), StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in query)
                {
                    Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwYarn);
                }
                tbIntex.Rows.Add(dwIntex);
                dwIntex = tbIntex.NewRow();
            }
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2GASTGA()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "GASTGA_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "GASTGA";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "GASTGA"
                        select dwMap;
            var query1 = from dwYarn in SetExt.Tables["VAFARBEN_EXT"].AsEnumerable()
                         select dwYarn;
            foreach (DataRow dwYarn in query1)
            {
                EventDispatcher.Instance.RaiseStatus("转换色号到GS07:" + dwYarn["VAFART"].ToString() + "-" + dwYarn["VAFFARBE"].ToString(), StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in query)
                {
                    Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwYarn);
                }
                tbIntex.Rows.Add(dwIntex);
                dwIntex = tbIntex.NewRow();
            }
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2GZKON()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "GZKON_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "GZKON";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "GZKON"
                        select dwMap;
            var query1 = from dwYarn in SetExt.Tables["VAFARBEN_EXT"].AsEnumerable()
                         select dwYarn;
            foreach (DataRow dwYarn in query1)
            {
                EventDispatcher.Instance.RaiseStatus("转换色号到GS07:" + dwYarn["VAFART"].ToString() + "-" + dwYarn["VAFFARBE"].ToString(), StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in query)
                {
                    Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwYarn);
                }
                tbIntex.Rows.Add(dwIntex);
                dwIntex = tbIntex.NewRow();
            }
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        
        public DataSet Convert2VAFARBEN()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "VAFARBEN_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "VAFARBEN";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "VAFARBEN"
                        select dwMap;
            var query1 = from dwYarn in SetExt.Tables["VAFARBEN_EXT"].AsEnumerable()
                         select dwYarn;
            foreach (DataRow dwYarn in query1)
            {
                EventDispatcher.Instance.RaiseStatus("转换色号到VA10:" + dwYarn["VAFART"].ToString() + "-" + dwYarn["VAFFARBE"].ToString(), StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in query)
                {
                    Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwYarn);
                }
                tbIntex.Rows.Add(dwIntex);
                dwIntex = tbIntex.NewRow();
            }
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2ABKOPF(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "ABKOPF_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "ABKOPF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "ABKOPF"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            EventDispatcher.Instance.RaiseStatus("转换生产单头AB04:" + dwData["ABPAUFKRS"].ToString() + "-" + dwData["ABPAUFLFD"].ToString(), StatusEventType.RunningProgress);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2ABPOS()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "ABPOS_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "ABPOS";
            bool newabk = false;
            DataRow dwIntex = tbIntex.NewRow();
            string processed = "";
            string krs = "", lfd = "",pos="";
            string SampleKRS = "T6";
            string SampleLFD = "005116";
            string SamplePOS = "010";
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "ABPOS"
                        select dwMap;
            var query1 = from dwABPOS_EXT in SetExt.Tables["ABPOS_EXT"].AsEnumerable()
                         select dwABPOS_EXT;
            foreach (DataRow dwABPOS_EXT in query1)
            {
                newabk = false;
                string ABKKDAUFTNR = dwABPOS_EXT["ABKKDAUFTNR"].ToString();
               SampleKRS= dwABPOS_EXT["SKRS"].ToString();
               SampleLFD= dwABPOS_EXT["SLFD"].ToString();
               SamplePOS= dwABPOS_EXT["SPOS"].ToString();
               SampleKRS = Right("  " + SampleKRS.Replace(" ", ""), 2);
               SampleLFD = Right("0000000" + SampleLFD.Replace(" ", "").TrimStart().TrimEnd(), 6);
               SamplePOS = Right("000" + SamplePOS.Replace(" ", ""), 3);
               if ((dwABPOS_EXT["ABPART"].ToString().Trim() + dwABPOS_EXT["ABPVAR"].ToString().Trim() + dwABPOS_EXT["ABPFARBE"].ToString().Trim()) == "")
                   continue;
                #region=====获取生产单号码======
                krs =Left(dwABPOS_EXT["ABPAUFKRS"].ToString()+"  ",2);
                lfd =Right("000000"+ dwABPOS_EXT["ABPAUFLFD"].ToString(),6);
                pos =Right("000"+ dwABPOS_EXT["ABPAUFPOS"].ToString(),3);
                DataRow rwTemp = dwABPOS_EXT.Table.Clone().Rows.Add();
                rwTemp["ABPMANDANT"] = "00";
                rwTemp["ABPAUFKRS"] = krs;
                rwTemp["ABPAUFLFD"] = lfd;
                rwTemp["ABPAUFPOS"] = pos;
                setResult = INTEXDBC.QueryABPOS(rwTemp,1);
                    if(setResult.Tables[0].Rows.Count>0)
                    {
                        rwTemp = setResult.Tables[0].Rows[0];
                        if (System.Convert.ToDecimal(rwTemp["ABPZUGKG"].ToString()) > 0)
                        {
                            EventDispatcher.Instance.RaiseStatus("转换掠过" + krs + "-" + lfd + "-" + pos + "  已经有原料留位", StatusEventType.RunningProgress);
                            continue;
                        }
                    }
                if (!processed.Contains(krs+lfd+";"))
                {
                    processed += krs + lfd + ";";
                    newabk = true;
                    dwIntexStd["ITXAB50KRS"] =krs;
                    /*
                    var query2 = from dwMap in SetExt.Tables["INTEX_MAP"].AsEnumerable()
                                 where dwMap["FILE"].ToString().Trim().Trim() == "INTEXMAP"
                                 && dwMap["FIELD"].ToString().Trim().Trim() == "ITXAB50LFD"
                                select dwMap;
                    foreach (DataRow dwRule in query2)
                    {
                        Convert2Intex.GetCovnertValue(ref dwIntexStd, dwRule);
                    }

                    Convert2Intex.SetExt.Tables["INTEXMAP"].Rows[0].ItemArray = dwIntexStd.ItemArray;*/
                }
                #endregion
                EventDispatcher.Instance.RaiseStatus("转换AB50:" + dwABPOS_EXT["ABPART"].ToString() + "-" + dwABPOS_EXT["ABPFARBE"].ToString(), StatusEventType.RunningProgress);
                DataSet SABPOS = INTEXDBC.QueryABPOS("00", SampleKRS, SampleLFD, SamplePOS);
                if (SABPOS.Tables.Count == 0)
                    return setReturn;
                DataTable TSABPOS = SABPOS.Tables[0];
                DataRow rwSABPOS = null;
                if (TSABPOS.Rows.Count > 0)
                {
                     rwSABPOS=TSABPOS.Rows[0];
                }
                foreach (DataRow dwRule in query)
                {
                    if (rwSABPOS != null)
                    {
                        string column = dwRule["FIELD"].ToString();
                        if (column == "ABPZLO" || column == "ABPZLB")
                        {
                            dwRule["DEFAULT"] = rwSABPOS[column];
                        }
                    }
                    Convert2Intex.GetCovnertValue(ref dwIntex, dwRule, dwABPOS_EXT);
                }
                tbIntex.Rows.Add(dwIntex);
                setResult = Convert2ABPR(dwIntex);
                setReturn.Merge(setResult);
                setResult = Convert2ABAUFM(dwIntex);
                setReturn.Merge(setResult);
                string yart="",yvar="",ycolor="";
                yart=dwIntex["ABPART"].ToString();
                yvar=dwIntex["ABPVAR"].ToString();
                ycolor=dwIntex["ABPFARBE"].ToString();
                if (SetExt.Tables.Contains("GASTGA_EXT"))
                {
                    SetExt.Tables["GASTGA_EXT"].Clear();
                }
                setResult = INTEXDBC.QueryGASTGA(yart, yvar, ycolor);
                setResult.Tables[0].TableName = "GASTGA_EXT";
                DataRow dwGASTGA = null;
                if (setResult.Tables[0].Rows.Count == 0)
                {
                    dwGASTGA = setResult.Tables[0].NewRow();
                    dwGASTGA["GAGGART"] = yart;
                    dwGASTGA["GAGGVAR"] = yvar;
                    dwGASTGA["GAGGFARBE"] = "000000    ";
                    dwGASTGA["GAGKGBRUT"] = "0001000000"; 
                    setResult.Tables[0].Rows.Add(dwGASTGA);                    
                }
                dwGASTGA = setResult.Tables[0].Rows[0];
                dwGASTGA["GAGKGBRUT"]=System.Convert.ToDouble(dwGASTGA["GAGKGBRUT"].ToString())*0.0001*0.01;
                SetExt.Merge(setResult);
                setResult = Convert2GAAUF713(dwIntex);
                setReturn.Merge(setResult);
                setResult = Convert2GAAUF310(dwIntex);
                
                setReturn.Merge(setResult);
                setResult = Convert2ABFOLGE(dwIntex, SampleKRS, SampleLFD, SamplePOS);
                setReturn.Merge(setResult);                
                if (newabk)
                {
                    setResult = Convert2ABKOPF(dwIntex);
                    setResult.Tables[0].Rows[0]["ABKKDAUFTNR"] = Left(ABKKDAUFTNR, 20);
                    setReturn.Merge(setResult);
                }               
                dwIntex = tbIntex.NewRow();
            }
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2ABPR(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "ABPR_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "ABPR";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "ABPR"
                        select dwMap;
            EventDispatcher.Instance.RaiseStatus("转换ABPR:" + dwData["ABPAUFKRS"].ToString() + "-" + dwData["ABPAUFLFD"].ToString() + "-" + dwData["ABPAUFPOS"].ToString(), StatusEventType.RunningProgress);
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2ABFOLGE(DataRow dwABPOS, string SampleKRS = "T6", string SampleLFD = "005116", string SamplePOS = "010")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            OracleCommand or1;
            DataRow dwLocal;
            try
            {                 
                //从Sample中将默认值代替，然后用ABPOS的值进行创建  
                if (!SetExt.Tables.Contains("ABPOS_NEW"))
                {
                    DataTable abpnew = dwABPOS.Table.Clone();
                    abpnew.TableName = "ABPOS_NEW";
                    SetExt.Merge(abpnew);
                }
                else 
                {
                    SetExt.Tables["ABPOS_NEW"].Clear();
                }
                DataRow dwABPNEW = SetExt.Tables["ABPOS_NEW"].NewRow();
                dwABPNEW.ItemArray = dwABPOS.ItemArray;
                SetExt.Tables["ABPOS_NEW"].Rows.Add(dwABPNEW);
                #region============APFOLGE========================= 
                DataSet SABFOLGE = INTEXDBC.QueryABFOLGE("00", SampleKRS, SampleLFD, SamplePOS);
                if (SABFOLGE.Tables.Count == 0)
                    return setReturn;
                DataTable TSABFOLGE = SABFOLGE.Tables[0];
                TSABFOLGE .TableName= "ABFOLGE_MODEL";
                #endregion
                DataSet setTemp = new DataSet();
                string mapID = "ABFOLGE_MAP";
                DataTable tbMapID = new DataTable();
                tbMapID = SetExt.Tables[mapID];
                DataTable tbIntex = new DataTable();
                tbIntex.TableName = "ABFOLGE";
                DataRow dwIntex = tbIntex.NewRow();
                var query = from dwMap in tbMapID.AsEnumerable()
                            where dwMap["FILE"].ToString().Trim().Trim() == "ABFOLGE"
                            select dwMap;
                EventDispatcher.Instance.RaiseStatus("转换ABFOLGE:", StatusEventType.RunningProgress);
                foreach (DataRow rwABFOLGE in TSABFOLGE.Rows)
                {
                    foreach (DataRow dwRule in query)
                    {
                        string column = dwRule["FIELD"].ToString();
                        dwRule["DEFAULT"] = rwABFOLGE[column];
                        Convert2Intex.GetCovnertValue(ref dwIntex, dwRule, rwABFOLGE);
                    }
                    tbIntex.Rows.Add(dwIntex);
                    dwIntex = tbIntex.NewRow();
                }
                setReturn.Merge(tbIntex); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet Convert2ABAUFM(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "ABAUFM_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "ABAUFM";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "ABAUFM"
                        select dwMap;
            EventDispatcher.Instance.RaiseStatus("转换ABAUFM:" + dwData["ABPAUFKRS"].ToString() + "-" + dwData["ABPAUFLFD"].ToString() + "-" + dwData["ABPAUFPOS"].ToString(), StatusEventType.RunningProgress);
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2GAAUF713(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "GAAUF713_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "GAAUF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "GAAUF"
                        select dwMap;
            EventDispatcher.Instance.RaiseStatus("转换GAAUF:" + dwData["ABPAUFKRS"].ToString() + "-" + dwData["ABPAUFLFD"].ToString() + "-" + dwData["ABPAUFPOS"].ToString(), StatusEventType.RunningProgress);
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2GAAUF310(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "GAAUF310_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "GAAUF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "GAAUF"
                        select dwMap;
            EventDispatcher.Instance.RaiseStatus("转换GAAUF:" + dwData["ABPAUFKRS"].ToString() + "-" + dwData["ABPAUFLFD"].ToString() + "-" + dwData["ABPAUFPOS"].ToString(), StatusEventType.RunningProgress);
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }

    }
}
